package com.demo.controller;

import com.demo.service.FunctionService;
import com.demo.service.ProService;
import com.demo.service.UserService;
import com.demo.vo.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AllocateController {
    @Autowired
    private FunctionService functionService;

    @Autowired
    private UserService userService;
    @Autowired
    private ProService proService;
    //查询当前项目的模块和功能信息
    @RequestMapping({"funcSelect"})
    @ResponseBody
    public List<Function> funcSelect(Integer prjId){
//        prjId = "114514";

        List<Function>  list = functionService.funcSelect(prjId);
        return list;
    }

    @RequestMapping(value = "updateAllocate")
    @ResponseBody
    Result updateAllocate(@RequestBody UpdateList list) {
        int status = 1;
        for (UpdateAllocateRequest i:
             list.getList()) {
            Integer  funcId = i.getFuncId();
            Integer userId = i.getUserId();
            status = status * functionService.updateAllocate(funcId, userId);
        }
//        int status = functionService.updateAllocate(funcId, userId);
        Result result = new Result();
        //删除成功时，Mybatis的返回值是1
        if (status == 1) {
            //前端约定状态码为0表示正常
            result.setCode(0);
        } else {
            result.setCode(1);
        }
//        System.out.println(status);
        return result;
    }
/*    @RequestMapping(value = "updateAllocate")
    @ResponseBody
    Result updateAllocate(String funcId, Integer userId){
        int status = functionService.updateAllocate(funcId,userId);
        Result result = new Result();
        //删除成功时，Mybatis的返回值是1
        if (status == 1){
            //前端约定状态码为0表示正常
            result.setCode(0);
        }else {
            result.setCode(1);
        }
//        System.out.println(status);
        return result;
    }*/

    @RequestMapping(value = "getUserList")
    @ResponseBody
    List<User> getUserList(){
        Map<String, Object> params = new HashMap();
        List<User> list = (List<User>) userService.list(params).get("list");
        List<User> returnList = new ArrayList<User>();
        for (User user:
             list) {
            User u = new User();
            u.setId(user.getId());
            u.setUsername(user.getUsername());
            returnList.add(u);
        }

        return returnList;
    }

    @RequestMapping(value = "getPrjList")
    @ResponseBody
    List<Pro> getPrjList(@Param("userId") Integer userId){

        Map<String, Object> params = new HashMap();
        List<Pro> list = functionService.selectPrjByUserId(userId);
        List<Pro> returnList = new ArrayList<Pro>();
        for (Pro pro:
                list) {
            Pro u = new Pro();
            u.setId(pro.getId());
            u.setProName(pro.getProName());
            returnList.add(u);
        }

        return returnList;
    }

    @RequestMapping(value = "getFunctionByUserId")
    @ResponseBody
    List<Function> selectFuncByUserId(@Param("userId")Integer userId){
        List<Function> list = functionService.selectFuncByUserId(userId);
        return list;
    }
}
